const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pid: 0,
    users: wx.getStorageSync('USERS')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      pid: options.pid
    });
    if (!that.data.users.id) {
      that.setData({
        users: wx.getStorageSync('USERS')
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    var that = this;
    var parmas = {
      "pid": that.data.pid,
      "uid": that.data.users.id,
      "access": e.detail.value.access,
      "secret": e.detail.value.secret
    };
    if (parmas.pid && parmas.pid > 0 && parmas.uid && parmas.uid > 0 && parmas.access && parmas.secret) {
      util.showLoading();
      util.wxPost({
        url: "/rest/addPlat", params: parmas,
        success: function (data) {
          util.hideToast();
          if (data.data.status == "000") {
            wx.showToast({
              title: "保成成功",
              icon: "success",
              duration: 1500,
              complete: function () {
                wx.navigateTo({
                  url: '/pages/users/plat',
                });
              }
            });
          } else {
            wx.showToast({
              title: "秘钥有误",
              icon: "failure",
              duration: 1500
            });
          }
        }
      });
    } else {
      wx.showToast({
        title: "秘钥有误不能空",
        icon: "failure",
        duration: 1500
      });
    }
  }
})